/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.config;

import com.mycompany.delivery.service.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author mhunek
 */
@Configuration
@EnableWebMvcSecurity
@ImportResource({"classpath:applicationContext.xml"})
//@EnableGlobalMethodSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("accUserDetailsService")
    private AccountServiceImpl accUserDetailsService;

    public AccountServiceImpl getAccUserDetailsService() {
        return accUserDetailsService;
    }

    public void setAccUserDetailsService(AccountServiceImpl accUserDetailsService) {
        this.accUserDetailsService = accUserDetailsService;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder registry) throws Exception {
        registry.userDetailsService(accUserDetailsService).passwordEncoder(passwordEncoder());
    }

    /* @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
     auth.inMemoryAuthentication().withUser("mkyong").password("123456").roles("USER");
     auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
     auth.inMemoryAuthentication().withUser("dba").password("123456").roles("DBA");
     }*/
    //.csrf() is optional, enabled by default, if using WebSecurityConfigurerAdapter constructor
    @Bean(name = "myAuthenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/account/admin/**").access("hasAnyRole('ROLE_EMPL', 'ROLE_COUR')")
                .antMatchers("/account/partlyVisible/**").access("hasAnyRole( 'ROLE_ADMIN', 'ROLE_EMPL', 'ROLE_COUR')")
               
                 .antMatchers("/delivery/**").access("hasAnyRole( 'ROLE_ADMIN', 'ROLE_EMPL')")
                 .antMatchers("/user/**").access("hasAnyRole( 'ROLE_ADMIN', 'ROLE_COUR')")
                 .antMatchers("/contact/**").access("hasAnyRole( 'ROLE_ADMIN', 'ROLE_EMPL',)")
                .and()
                .formLogin().loginPage("/account/login").failureUrl("/account/login?error")
                .usernameParameter("username").passwordParameter("password")
                .and()
                .logout().logoutSuccessUrl("/account/login?logout").and()
                .exceptionHandling().accessDeniedPage("/admin/403")
                .and()
                .csrf().disable();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }
}
